#!/bin/bash

PWD=`pwd`
user=$USER
home=$HOME
hostname=`hostname`

HOST1NAME1="vanxum02"
HOST1NAME2="ubuntu"

function git_branch {
    branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
    if [ "${branch}" != "" ];then
        if [ "${branch}" = "(no branch)" ];then
            branch="(`git rev-parse --short HEAD`...)"
        fi
        echo "($branch)"
    fi
}

function ssh_info {
    local ssh_info="`echo $SSH_CONNECTION | awk '{print $3}'`"
    if [ -n "$ssh_info" ];then
        echo "in $ssh_info "
    fi
}

# tmux
function tmux_new()
{
	local session=$1
	echo "tmux new $session session"
	tmux ls | grep "$session"
	if [ $? -ne 0 ]; then
		(tmux new -s $session -d) #后台运行
	else
		echo -e "\t$session session was created ..."
	fi
	echo "tmux new OK"
}

function tmux_startup()
{
	echo "mux_startup TMUX=$TMUX"
	if which tmux 2>&1 >/dev/null; then
		tmux_new "init_session"
		tmux_new "other_session"

		#每次连接都进入init_session会话
		test -z "$TMUX" && (tmux attach -t init_session)
	fi
    #screenfetch 2>/dev/null
	run-parts /etc/update-motd.d/
}

# history格式
USER_IP=`who -u -m | awk '{print $NF}' | sed 's/[()]//g'`
USER=`whoami`
export HISTTIMEFORMAT="[%F-%T] [$USER:$USER_IP] "

#习惯的使用别名
alias vi='vim --noplugin'
alias grep='grep --color'
alias man='viman'

# https://astyle.sourceforge.net/astyle.html
# --style=linux : linux风格
# --indent=force-tab=4 --convert-tabs： Tab缩进并强制转换为4个空格，与linux内核缩进一致
# --pad-oper：在操作符号两边添加空格字符
# --unpad-paren：去除括号内壁多余空格
# --pad-header： 表示”if”、”for”、”while”等keyword右边添加一个空格
# --break-blocks=all：在标题块(关键字上方)周围填充空行（例如“if”、“for”、“while”…）。将结束头块（例如“else”、“catch”）视为独立块
# --indent-continuation=4: 函数名及参数分为两行时第二行缩进
# --indent-after-parens: 函数多行与--indent-continuation配合使用
# --indent-preproc-block: 嵌套宏定义进行缩进
# --delete-empty-lines: 删除多余空行
# --align-pointer=name: 指针*号靠近名字侧
alias astyle-c='astyle --style=linux --indent=force-tab=4 --convert-tabs --pad-oper --unpad-paren --pad-header --indent-continuation=2 --indent-after-parens --indent-preproc-block --align-pointer=name'
alias astyle-batch='astyle --quiet --options=~/.work_env/tools/configs/astylerc *.c *.h'

if [ hash colordiff 2> /dev/null ]; then
    echo "I require colordiff but it's not installed.  Aborting."
else
    alias diff='colordiff'
fi

alias m-build='sudo sshfs  wq@172.16.200.101:/home/xx /home/wq/build-dir -o gid=1000,uid=1000,allow_other'
#alias m-share='sudo mount -t cifs -o username=xxx,domain=xxx,password=xxx,iocharset=utf8,dir_mode=0755,file_mode=0666,uid=wq,gid=wq //192.186.1.1/home /home/wq/share; ls /home/wq/share'
# https://motrix.app && chmod +x Motrix-1.1.3-x86_64.AppImage
#alias motrix='$HOME/software/Motrix-1.1.3-x86_64.AppImage'
alias enca-utf-8='enca -L zh_CN -x UTF-8'

# Git Repo 镜像, https://mirrors.tuna.tsinghua.edu.cn/help/git-repo/
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

# for android build, 为了去除所有本地化的设置，让命令能正确执行
#export LC_ALL=C

# for tmux and vim, true color真彩显示
if [ -n "$TMUX" ]; then
	# tmux终端内部配置
	export TERM=tmux-256color
else
	# 普通终端配置
	export TERM=xterm-256color
fi

# "CTRL-S：挂起终端（XON） "CTRL-Q：恢复终端（XOFF） "要禁止这些键盘映射
stty -ixon

# 配置终端英文
if [ "$TERM"="linux" ] ;then
    export LANGUAGE=en_US
    export LANG=en_US.UTF-8
fi

# 终端输入提示
if [ x$hostname = x$HOST1NAME1 ]; then
    PS1='\A [\u@\h]\w\n↪ $(ssh_info)=>$(git_branch)\$'
    export LESSCHARSET=utf-8 #git log中文乱码
    tmux_startup
elif [ x$hostname = x$HOST1NAME2 ]; then
    PS1='\[\e[36m\]\A \[\e[32m\][\u\[\e[31m\]@\[\e[33m\]\h\[\e[32m\]]\w\n↪ $(ssh_info)=>\[\033[01;36m\]$(git_branch)\[\e[0m\]\[\e[31m\]\$\[\e[0m\]'
    tmux_startup
else
    #PS1='\u@\h \[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\]\$ '
    PS1='\[\e[36m\]\A \[\e[32m\][\u\[\e[31m\]@\[\e[33m\]\h\[\e[32m\]]\w\n↪ $(ssh_info)=>\[\033[01;36m\]$(git_branch)\[\e[0m\]\[\e[31m\]\$\[\e[0m\]'
    #PS1="\[\e[36m\]\A \[\e[32m\][\u\[\e[31m\]@\[\e[33m\]\h\[\e[32m\]]\w\[\e[31m\]\$\[\e[0m\]"
    if [ -n "$SSH_CONNECTION" ]; then
        tmux_startup
    fi
fi
